package com.briup.ptt.service.Impl;

import com.briup.ptt.mapper.*;
import com.briup.ptt.mapper.ext.AnimalExtMapper;
import com.briup.ptt.mapper.ext.CountExtMapper;
import com.briup.ptt.mapper.ext.IssueRecordExtMapper;
import com.briup.ptt.service.AnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * @author 杰瑞
 * @date 2025/8/14
 */
@Service
public class AnalysisServiceImpl implements AnalysisService {
    @Autowired
    private FenceHouseMapper fenceHouseMapper;

    @Autowired
    private HurdlesMapper hurdlesMapper;

    @Autowired
    private AnimalMapper animalMapper;

    @Autowired
    private FreezerMapper freezerMapper;

    @Autowired
    private AccountMapper accountMapper;

    @Autowired
    private CountExtMapper countExtMapper;

    @Autowired
    private AnimalExtMapper animalExtMapper;

    @Autowired
    private IssueRecordExtMapper issueRecordExtMapper;



    @Override
    public Map<String, List<Object>> countNum() {
//        Integer fenceHouseNum = countExtMapper.countFenceHouseNum();
//        Integer hurdlesNum = countExtMapper.countHurdlesNum();
//        Integer animalNum = countExtMapper.countAnimalNum();
//        Integer freezerNum = countExtMapper.countFreezerNum();
//        Integer accountNum = countExtMapper.countAccountNum();

//        Map<String, List<Object>> map = new HashMap<>();
//        List<String> name = Arrays.asList("栏舍"," 栏圈"," 动物"," 冷库"," 员工");
//        List<Integer> value = Arrays.asList(fenceHouseNum,hurdlesNum,animalNum,freezerNum,accountNum);
//        map.put("name", new ArrayList<>(name));
//        map.put("value", new ArrayList<>(value));

        Map<String, Object> mapList = countExtMapper.countNum();
        List<Object> name = new ArrayList<>(mapList.keySet());
        List<Object> value = new ArrayList<>(mapList.values());
        Map<String, List<Object>> map = new HashMap<>();
        map.put("name", name);
        map.put("value", value);
        return map;
    }

    @Override
    public Map<String, Object> countWeight() {
        Map<String, Map<String, ?>> result = new HashMap<>();
        result.values().stream().collect(Collectors.toMap(m -> m.get("type"),
                m -> m.get("num")));

        return null;
    }

    @Override
    public Map<String, Object> countDisease() {
        List<Map<String, Object>> mapList = countExtMapper.countDisease();
        Map<String, Object> result = new LinkedHashMap<>();
        for(Map<String, Object> map : mapList){
            result.put((String) map.get("type"), ((Number) map.get("total")).intValue());
        }
        return result;
    }

    @Override
    public Map<String, List<Object>> countSales() {
//        List<Map<String, Object>> result = issueRecordExtMapper.countSales();
        Map<String,Map<String, ?>> result = issueRecordExtMapper.countSales();

//        String[] nameValue = {"1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"};
//      1.列举(数组或者集合)， 2.  for循环 3.stream流
//        List<String> moths2 = Stream.iterate(1, n -> n + 1)
//                .limit(12).map(n -> n + "月")
//                .collect(Collectors.toList());

        List<Object> months = IntStream.rangeClosed(1, 12)
                .mapToObj(n -> n + "月")
                .collect(Collectors.toList());


//        int[] value = new int[12];

        List<Object> value = Stream.iterate(1, n -> n+1)
                .limit(12)
                .map(n ->{
                    return null;
                })
                .collect(Collectors.toList());

        //外部遍历
//        for(Map<String, Object> maps : mapList){
//            value[(int) maps.get("month")] = ((Number) maps.get("total")).intValue();
//            value.set((Integer) maps.get("month"),maps.get("total"));
//        }

//        Map<String, List<Object>> map = new HashMap<>();
//        map.put("name", months);
//        map.put("value", value);

        return Map.of("name", months, "value", value);
    }
}
